package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.controller.AliyunOSSController;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author 山外小王
 * @Description
 * @Date 2024/04/15/15:57
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setMealDao.selsctConditions(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //用户点击新增时，图片为有效图片
        if (setmeal.getImg()!=null){
            //获取图片名称
            String UserFileName = setmeal.getImg();
            redisTemplate.opsForSet().remove(AliyunOSSController.ALL_IMG_FILE,UserFileName);
        }

        setMealDao.insert(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetmealCheckgroup(checkgroupId,setmeal.getId());
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.selectById(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        //编辑套餐数据
        setMealDao.update(setmeal);
        //删除关联信息表
        setMealDao.deleteSetmealCheckgroup(setmeal.getId());
        //增加关联信息
        for (Integer checkGroupId : checkGroupIds) {
            setMealDao.insertSetmealCheckgroup(checkGroupId,setmeal.getId());
        }
    }
    @Override
    public void deleteSetmealById(Integer id) {
        setMealDao.deleteById(id);
    }


/**
 * 用户端
 */
    /**
     * c端
     * 套餐展示
     * @return
     */
    @Override
    public List<Setmeal> getSetmeal() {
        return setMealDao.selectAll();
    }

    @Override
    public Setmeal findDetailById(Integer id) {
        //查询套餐详细  里面有检查组详情  检查组有检查项详情 现有套餐id
        //根据id查询套餐信息
        Setmeal setmeal = setMealDao.selectById(id);
        //套餐信息缺List<CheckGroup> checkGroups
        //根据套餐id查询所有的检查组id
        Integer[] checkGroupIds = checkGroupDao.selectCheckGroupIdssById(id);
        //构建检查组列表
        List<CheckGroup> checkGroupList = new ArrayList<>();
        //遍历检查组id
        for (Integer checkGroupId : checkGroupIds) {
            CheckGroup checkGroup = checkGroupDao.selectCheckGroupById(Long.valueOf(checkGroupId));
            //检查组信息缺List<CheckItem> checkItems
            //根据检查组id查询检查项id数组
            Integer[] checkItemIds = checkGroupDao.selectCheckGroupIdsById(Long.valueOf(checkGroupId));
            //遍历检查项id
            //构建检查项列表
            List<CheckItem> checkItemList = new ArrayList<>();
            for (Integer checkItemId : checkItemIds) {
                //查询检查项
                CheckItem checkItem = checkItemDao.selectById(checkItemId);
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
            checkGroupList.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }

}
